//
//  LS_FindViewModel.m
//  breadPro
//
//  Created by 刘文超 on 2019/7/5.
//  Copyright © 2019 刘文超. All rights reserved.
//

#import "LS_FindViewModel.h"
#import "LS_SoreItemModel.h"
#import "LS_PublishAdViewController.h"
#import "LS_WebViewModel.h"
#import "LS_WebViewController.h"
#import "LS_ActivitysModel.h"
#import "BZFireworkAnimationButton.h"
@interface LS_FindViewModel()
@property(nonatomic,strong)id<LS_ViewModelService> services;
@end
@implementation LS_FindViewModel
-(instancetype)initWithService:(id<LS_ViewModelService>)services
{
    if (self =[super init]) {
        _services = services;
        [self initizalize];
    }return self;
}
-(void)initizalize
{
    
    self.bannerArr = @[];
    self.newsArr = @[];
    self.soreItemArr = @[];
    self.advertisesItem = @[];
    self.advertise_pic = @{};
    
    
    self.leftNavitemCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [subscriber sendCompleted];
            return nil;
        }];
    }];
    //请求数据
    self.foundCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        NSMutableDictionary *param = (NSMutableDictionary*)input;
        
        return [[[self.services getFindService] ls_requestDataWithUrl:Found_URL WithParam:param] doNext:^(id result) {
            self.bannerArr =[NSArray arrayWithArray:result[FoundBannerkey]] ;
            self.newsArr = [NSArray arrayWithArray:result[FoundNewskey]];
            self.soreItemArr = [NSArray arrayWithArray:result[FoundSorekey]];
            self.advertisesItem =[NSArray arrayWithArray:result[FoundActivitykey]] ;
            self.advertise_pic = result[FoundAdd_activity_pic];
            self.foundDataDic = result;
            NSLog(@"result------%@",result);
        }];
    }];
    
    self.didSelectsSoreItemCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        LS_SoreItemModel *model = input;
         NSLog(@"点击了分类数据---------%@-----%@",input,model.sc_name);
        [LS_ShowMessageView showSuccessWithMessage:kFORMAT(@"点击了%@分类",model.sc_name)];
        return [RACSignal empty];
    }];
    
    self.didMarqueeCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        NSLog(@"点击了跑马灯");
        NSString *url = @"https://www.baidu.com";
        LS_WebViewModel *model =[[LS_WebViewModel alloc]initWithServices:self.services param:@{WebTitlekey:@"公告详情",WebViewTypekey:@(LSWebTypeMainPageDetialType),WebNavBarStyleTypekey:@(LSWebNavBarStyleTypeNomer),RequestURLkey:url}];
        LS_WebViewController *VC =[[LS_WebViewController alloc]initWithViewModel:model];
        [self.MyVc.navigationController pushViewController:VC animated:YES];
        return [RACSignal empty];
    }];
    
    self.didPublishMessageCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        NSLog(@"点击发布广告");
        LS_PublishAdViewController *VC =[LS_PublishAdViewController new];
        VC.title = @"发布广告";
        [self.MyVc.navigationController pushViewController:VC animated:YES];
        return [RACSignal empty];
    }];
    
    self.didBannerCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        NSLog(@"点击了轮播图");
        NSString *url = input[@"url"];
        LS_WebViewModel *model =[[LS_WebViewModel alloc]initWithServices:self.services param:@{WebTitlekey:@"轮播详情",WebViewTypekey:@(LSWebTypeDestinDetialType),WebNavBarStyleTypekey:@(LSWebNavBarStyleTypeNomer),RequestURLkey:url}];
        LS_WebViewController *VC =[[LS_WebViewController alloc]initWithViewModel:model];
        [self.MyVc.navigationController pushViewController:VC animated:YES];
        return [RACSignal empty];
    }];
    
    //点赞的方法
    self.pariaZanCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        
        LS_ActivitysModel *model = input[@"model"];
        HJLayoutBtn *button = input[@"button"];
        BZFireworkAnimationButton *fireBtn = input[@"BZFireworkbtn"];
        NSMutableDictionary *dict =[[NSMutableDictionary alloc]init];
        dict[@"activity_id"] = model.ID;
        if ([model.status isEqualToString:@"2"]) {
            dict[@"type"] = @"2";
        }else{
            dict[@"type"] = @"1";
        }
        return [[[self.services getFindService] ls_praiseZanWithUrl:Found_PraiseURL WithParam:dict] doNext:^(id x) {
            NSLog(@"*******%@",x);
            if ([x[@"code"] isEqualToString:@"204"]) {
                [LS_ShowMessageView showErrorWithMessage:x[@"msg"]];
            }else{
                [LS_ShowMessageView showSuccessWithMessage: [model.status isEqualToString:@"2"] ? @"点赞成功" : @"取消点赞"];
                model.status = [model.status isEqualToString:@"1"] ? @"2" : @"1";
                button.selected = [model.status isEqualToString:@"1"] ?  YES : NO;
                fireBtn.selected = [model.status isEqualToString:@"1"] ?  YES : NO;
                fireBtn.hidden = NO;
                button.hidden = YES;
                if ([model.status isEqualToString:@"2"]) {
                    [fireBtn popInsideWithDuration:3];
                }else{
                  [fireBtn animate];
                  [fireBtn popOutsideWithDuration:3];
                }
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    fireBtn.hidden = YES;
                    button.hidden = NO;
                });
            }
        }];
    }];
    
}
@end
